package dao;

import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import dto.QuestionDTO;;

public class QuestionDAO extends DatabaseHelper {
	
    public static final String TABLE_QUESTION = "Question";
    
    public static final String QUESTION_ID = "ID";
    public static final String QUESTION_CONTENT = "Content";
    public static final String QUESTION_TITLEID = "TitleID";
    
	public QuestionDAO(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		
	}

    public void addQuestion(QuestionDTO questionDTO) 
    {
    	// Open database
        try {
	        ContentValues values = new ContentValues();
	        
	        values.put(QUESTION_CONTENT, questionDTO.getContent());
	        values.put(QUESTION_TITLEID, questionDTO.getTitleID());
	        
	        // Inserting rows
	        open();
	        DatabaseHelper.myDataBase.insert(TABLE_QUESTION, null, values);
	
	        // Close database connection
	        DatabaseHelper.myDataBase.close();
        }
        catch (Exception ex) {}
    }         
    
    public QuestionDTO getQuestion(int id) {
        try {
	        Cursor cursor = DatabaseHelper.myDataBase.query(TABLE_QUESTION, new String[] { QUESTION_ID,
	        		QUESTION_CONTENT, QUESTION_TITLEID}, QUESTION_ID + "?",
	            new String[] {String.valueOf(id) }, null, null, null, null);
	        if (cursor != null)
	            cursor.moveToFirst();
	        
	        QuestionDTO question = new QuestionDTO(
	                Integer.parseInt(cursor.getString(0)),
	                cursor.getString(1), 
	                Integer.parseInt(cursor.getString(2)));
	        return question;
        }
        catch (Exception e) {
        	return null;
        }
    }
        
    public List<QuestionDTO> getAllQuestions() {
    	try {
	        List<QuestionDTO> questionList = new ArrayList<QuestionDTO>();
	        
	        // Select all query
	        String selectQuery = "SELECT * FROM " + TABLE_QUESTION;
	        open();
	        Cursor cursor = DatabaseHelper.myDataBase.rawQuery(selectQuery, null);
	        
	        // Looping through all rows and adding to list
	        
	        if (cursor.moveToFirst()) {
	            do {
	            	QuestionDTO question = new QuestionDTO(
	    	                Integer.parseInt(cursor.getString(0)),
	    	                cursor.getString(1), 
	    	                Integer.parseInt(cursor.getString(2)));
	            
	            	questionList.add(question);
	            } while (cursor.moveToNext());
	        }
	        close();
	        return questionList;
    	}
    	catch (Exception e) {
			return null;
		}
    }
    
    public boolean updateQuestion(QuestionDTO questionDTO) {
        try {
	        ContentValues values = new ContentValues();
	        
	        values.put(QUESTION_ID, questionDTO.getID());
	        values.put(QUESTION_CONTENT, questionDTO.getContent());
	        values.put(QUESTION_TITLEID, questionDTO.getTitleID());
	        open();
	        // Updating row
	        int isSucceed = DatabaseHelper.myDataBase.update(TABLE_QUESTION, values, QUESTION_ID + " = ?",
	                new String[] { String.valueOf(questionDTO.getID()) });
	        close();
	        if (isSucceed > 0) {
	        	return true;
	        }
	        	
	        return false;
        }
        catch (Exception ex) { 
        	return false; 
        }
    }
    
    public void deleteQuestion(QuestionDTO questionDTO) {
    	try {
    		DatabaseHelper.myDataBase.delete(TABLE_QUESTION, QUESTION_ID + " = ?",
                new String[] { String.valueOf(questionDTO.getID()) });
    		DatabaseHelper.myDataBase.close();
    	}
    	catch (Exception ex) {}
    }
    
    public void deleteAllQuestions() 
    {
    	try {
    		DatabaseHelper.myDataBase.delete(TABLE_QUESTION, null, null);
    	}
    	catch (Exception ex) {}
    }
    
    public int getQuestionCount()
    {
    	try {
	        String countQuery = "SELECT * FROM " + TABLE_QUESTION;
	        SQLiteDatabase db = this.getReadableDatabase();
	        Cursor cursor = db.rawQuery(countQuery, null);
	        cursor.close();
	        
	        // Return count
	        return cursor.getCount();
    	}
    	catch (Exception ex) {return -1;}
    }
}
